//https://leetcode.cn/problems/maximum-value-at-a-given-index-in-a-bounded-array/

class Solution {
public:
    int maxValue(int n, int index, int maxSum) {
        int dt=maxSum-n;//先让所有数都为1
        int l=0,r=maxSum;
        while(l<r)
        {
            int mid=(l+r+1)/2;
            long long a=min(index,mid-2),b=min(n-index,mid-1);//等差数列的项数
            if(a*(mid-2+(mid-a-1))/2+b*(mid-1+mid-b)/2<=dt)l=mid;//验证等差和与dt的大小
            else r=mid-1;
        }
        return l;
    }
};